/*
	File:		Tiles.cppj
	Course:		SGD 1301
	Author:		Peter Nguyen
	Purpose:	CTiles class handles base tiles class
*/
#include "Tiles.h"
#include "../SGD Wrappers/CSGD_Direct3D.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "Game.h"
#include "Ship.h"
#include "Laser.h"
#include "../SGD Wrappers/CSGD_MessageSystem.h"
#include "DestroyEntityMessage.h"
CTiles::CTiles()
{
	m_eType = ENT_TILES;
}

CTiles::~CTiles()
{
}

void CTiles::Update( float fElapsedTime )
{
}

void CTiles::Render( void )
{

	CGame* gamePtr = CGame::GetInstance();

	POINT start = {(int)GetPosX()*GetWidth()-gamePtr->GetCameraX(),(int)GetPosY()*GetHeight()-gamePtr->GetCameraY()};
	rotate_point(gamePtr->GetWidth()/2.0f,gamePtr->GetHeight()/2.0f,gamePtr->GetCameraRotation(),start);

	RECT srcRECT = {0,0,512,512};
	float scale = 508.0f / GetWidth();

	CSGD_TextureManager::GetInstance()->Draw( GetImageID(),
		start.x, start.y,
		1.0f/scale,1.0f/scale,&srcRECT, 
		GetWidth() *0.5f,GetHeight()*0.5f,gamePtr->GetCameraRotation());
}

bool CTiles::CheckCollision( IEntity* pOther )
{
	if((pOther->GetType()==CEntity::ENT_INPUTSHIP)||(pOther->GetType()==CEntity::ENT_AISHIP))
	{
		CShip* pShip = dynamic_cast<CShip*>(pOther);
		float wH = m_nWidth/2.0f;
		float hH = m_nWidth/2.0f;
		float xCen = m_fPosX * m_nWidth  + wH;
		float yCen = m_fPosY * m_nHeight + hH;
		float shipX = pShip->GetPosX();
		float shipY = pShip->GetPosY();
		float shipWH = pShip->GetWidth()/2.0f;
		float shipHH = pShip->GetHeight()/2.0f;

		if(xCen+wH<shipX-shipWH)
			return false;
		if(xCen-wH>shipX+shipWH)
			return false;
		if(yCen+hH<shipY-shipHH)
			return false;
		if(yCen-hH>shipY+shipHH)
			return false;

		//////////////////////
		//Collided
		shipX = pShip->_pervPosX;
		shipY = pShip->GetPosY();
		for(;;)//kinda like a goto to skip lines of codes
		{
			if(xCen+wH<shipX-shipWH)
				break;
			if(xCen-wH>shipX+shipWH)
				break;
			if(yCen+hH<shipY-shipHH)
				break;
			if(yCen-hH>shipY+shipHH)
				break;
			pShip->SetPosY(pShip->_pervPosY);
			pShip->SetVelY(0);
			break;
		}
		
		shipX = pShip->GetPosX();
		shipY = pShip->_pervPosY;
		for(;;)//kinda like a goto to skip lines of codes
		{
			if(xCen+wH<shipX-shipWH)
				break;
			if(xCen-wH>shipX+shipWH)
				break;
			if(yCen+hH<shipY-shipHH)
				break;
			if(yCen-hH>shipY+shipHH)
				break;
			pShip->SetPosX(pShip->_pervPosX);
			pShip->SetVelX(0);
			break;
		}

		pShip->SetSpeed(pShip->GetSpeed()*0.95f);

		return true;
	}
	else if(CLaser* pLaser = dynamic_cast<CLaser*>(pOther))
	{
		float wH =		(float)(m_nWidth/2*0.8f				);
		float hH =		(float)(m_nWidth/2*0.8f				);
		float xCen =	(float)(m_fPosX * m_nWidth  + wH	);
		float yCen =	(float)(m_fPosY * m_nHeight + hH	);
		float shipX =	(float)(pLaser->GetPosX()			);
		float shipY =	(float)(pLaser->GetPosY()			);
		float shipWH =	(float)(pLaser->GetWidth()/2		);
		float shipHH =	(float)(pLaser->GetHeight()/2		);

		if(xCen+wH<shipX-shipWH)
			return false;
		if(xCen-wH>shipX+shipWH)
			return false;
		if(yCen+hH<shipY-shipHH)
			return false;
		if(yCen-hH>shipY+shipHH)
			return false;

		CSGD_MessageSystem::GetInstance()->SendMsg(new CDestroyEntityMessage(pLaser));
	}
	return false;
}